package org.bonorsoft.modules.education.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.system.query.QueryGenerator;
import org.bonorsoft.common.util.DateUtils;
import org.bonorsoft.modules.education.entity.Teacher;
import org.bonorsoft.modules.education.service.ITeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.bonorsoft.common.aspect.annotation.AutoLog;
import org.bonorsoft.common.system.base.controller.BonorController;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Description: 教师信息
 * @Author: WilliamYe
 * @Date:   2020-12-07
 * @Version: V1.0
 */
@Slf4j
@Controller
@RequestMapping("/education/teacher")
public class TeacherController extends BonorController<Teacher, ITeacherService> {
    private String prefix = "education/teacher";

	@Autowired
	private ITeacherService teacherService;

	/**
	*首页
	*/
	@RequiresPermissions("education:teacher:view")
    @GetMapping()
    public String index()
    {
     return prefix + "/teacher";
    }

	/**
	 * 分页列表查询
	 *
	 * @param teacher
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "教师信息-分页列表查询")
	@RequiresPermissions("education:teacher:list")
	@GetMapping(value = "/list")
	@ResponseBody
	public Result<?> queryPageList(Teacher teacher,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<Teacher> queryWrapper = QueryGenerator.initQueryWrapper(teacher, req.getParameterMap());
		Page<Teacher> page = new Page<Teacher>(pageNo, pageSize);
		IPage<Teacher> pageList = teacherService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

    /**
    * 添加页面
    *
    */
    @GetMapping("/add")
    public String add()
    {
     return prefix + "/add";
    }

	/**
	 * 添加
	 *
	 * @param teacher
	 * @return
	 */
	@AutoLog(value = "教师信息-添加")
	@RequiresPermissions("education:teacher:add")
	@PostMapping(value = "/add")
    @ResponseBody
	public Result<?> add(@Validated Teacher teacher) {
		teacher.setCreateBy(getLoginUserName());
		teacher.setCreateTime(DateUtils.getDate());
		teacherService.save(teacher);
		return Result.ok("添加成功！");
	}

    /**
    * 编辑页面
    */
	@GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap map)
    {
     map.put("eduTeacher", teacherService.getById(id));
     return prefix + "/edit";
    }

	/**
	 * 编辑
	 *
	 * @param teacher
	 * @return
	 */
	@AutoLog(value = "教师信息-编辑")
	@RequiresPermissions("education:teacher:edit")
	@PostMapping(value = "/edit")
	@ResponseBody
	public Result<?> edit(@Validated Teacher teacher) {
		teacher.setUpdateBy(getLoginUserName());
		teacher.setUpdateTime(DateUtils.getDate());
		teacherService.updateById(teacher);
		return Result.ok("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "教师信息-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		teacherService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "教师信息-批量删除")
	@RequiresPermissions("education:teacher:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	@ResponseBody
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.teacherService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "教师信息-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Teacher teacher = teacherService.getById(id);
		return Result.ok(teacher);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param teacher
    */
	@AutoLog(value = "教师信息-导出excel")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Teacher teacher) {
		return super.exportXls(request, teacher, Teacher.class, "教师信息");
    }

    /**
    * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
	@AutoLog(value = "教师信息-导入数据", operateType = 5)
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	@ResponseBody
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return importExcel(request, response, Teacher.class);
    }

	 /**
	* 模板下载
	* @return
	*/
 	@AutoLog(value = "教师信息-模板下载", operateType = 6)
	@RequestMapping(value = "/importTemplate")
	public ModelAndView importTemplate() {
	return super.importTemplateXls(Teacher.class ,"教师信息");
	}

}
